package com.xb.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xb.model.domain.UsersInfo;
import com.xb.system.mapper.DistinctUsersInfoMapper;
import com.xb.system.mapper.UsersInfoMapper;
import com.xb.system.service.UsersInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (Users)表服务实现类
 */
@Service
@Slf4j
public class UsersInfoServiceImpl extends ServiceImpl<UsersInfoMapper, UsersInfo> implements UsersInfoService {
    @Resource
    private UsersInfoMapper usersInfoMapper;
    
    @Resource
    private DistinctUsersInfoMapper distinctUsersInfoMapper;

    public boolean checkUserMatch(String phone, String username) {
        //先去分表中查询，查不到再去distinct_users_info表中查询
        if (usersInfoMapper.checkUserMatch(phone, username) > 0) {
            return true;
        } else return distinctUsersInfoMapper.checkUserMatch(phone, username) > 0;
    }
    
    public void createIndexes() {
        log.info("正在创建分表的索引");
        usersInfoMapper.createUserInfoIndexes();
        log.info("索引创建成功");
    }
    
    public void dropIndexes() {
        usersInfoMapper.dropUserInfoIndexes();
        log.info("索引删除成功");
    }

    @Override
    public long totalIndexesRows() {
        long totalRows = 0;
        for (int i = 0; i < 200; i++) {
            totalRows += usersInfoMapper.totalRows(i);
        }
        System.out.println("重复表"+distinctUsersInfoMapper.selectCount(null));
        return totalRows + distinctUsersInfoMapper.selectCount(null);
    }

}

